package icu.javacg.storage.controller;

import icu.javacg.storage.pojo.FireEquipment;
import icu.javacg.storage.pojo.vo.DataTableResults;
import icu.javacg.storage.pojo.vo.OperationQueryParam;
import icu.javacg.storage.pojo.vo.ReturnResult;
import icu.javacg.storage.service.FireEquipmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/fireEquipment")
@CrossOrigin
@Tag(name = "消防管理接口/周炳霖")
public class FireEquipmentController {

    @Resource
    FireEquipmentService fireEquipmentService;

    @GetMapping("/page")
    @Operation(summary = "消防器材分页")
    @Parameter(name = "param", description = "封装了查询起始下标，每页展示的数据数量，排序属性，排序方式的对象", required = true)
    public DataTableResults list(OperationQueryParam param){
        if (param == null) return null;
        int start = param.getStart();
        int offset = param.getOffset();
        String sortPro = param.getSortPro();
        String orderDir = param.getSortType() == 0 ? "ASC" : "DESC";
        return fireEquipmentService.selectPermissionListPage(start, offset, sortPro, orderDir);
    }
    @GetMapping("/allList")
    @Operation(summary = "查询全部的消防信息")
    public ReturnResult<List<FireEquipment>> AllFire(){
         List<FireEquipment> list = fireEquipmentService.list();
         if (list.size() == 0){
             return new ReturnResult<>(2510,"查询失败");
         }
            return new ReturnResult<>(2010,"查询成功",fireEquipmentService.list());
    }
    @PutMapping("/insert")
    @Operation(summary = "添加消防信息")
    public ReturnResult<FireEquipment> addFireEquipment(@RequestBody FireEquipment fireEquipment){
        return fireEquipmentService.addFire(fireEquipment);
    }
    @PostMapping("/update")
    @Operation(summary = "修改消防信息")
    public ReturnResult<Boolean> updateFire(@RequestBody FireEquipment fireEquipment){
         boolean update = fireEquipmentService.updateById(fireEquipment);
        if (update) {
            return new ReturnResult<>(2520,"修改成功",update);
        }
            return new ReturnResult<>(2020,"修改失败",update);
    }
    @DeleteMapping("/delete")
    @Operation(summary = "删除消防信息")
    public ReturnResult<Boolean> deleteFire(@RequestParam("id") String id){
         boolean removeById = fireEquipmentService.removeById(id);
         if (removeById){
             return new ReturnResult<>(2530,"删除成功",removeById);
         }
            return new ReturnResult<>(2030,"删除失败",removeById);
    }
}
